package com.wanxi.config.security;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("authenticationEntryPoint")
public class AuthenticationEntryPointConfig implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        if(authException.getLocalizedMessage().equals("Bad credentials")&&request.getRequestURI().equals("/manager/login")){
            request.getRequestDispatcher("/errorLogin").forward(request,response);
        } else if (authException.getLocalizedMessage().equals("User is disabled")) {
            request.getRequestDispatcher("/errorLogin").forward(request,response);
        }
        request.getRequestDispatcher("/noLogin").forward(request,response);
    }

}
